package com.cmc.cloud.cmclink.doc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cmc.cloud.cmclink.doc.api.document.emums.PackingEnum;
import com.cmc.cloud.cmclink.doc.constants.CommonConstants;
import com.cmc.cloud.cmclink.doc.constants.DgCommodityConstants;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.SpecialCargoApplyErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.SpecialCargoDgErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.SpecialCargoCasConvert;
import com.cmc.cloud.cmclink.doc.convert.SpecialCargoDgConfConvert;
import com.cmc.cloud.cmclink.doc.convert.SpecialCargoDgConvert;
import com.cmc.cloud.cmclink.doc.convert.SpecialCargoDgPackingConvert;
import com.cmc.cloud.cmclink.doc.entity.*;
import com.cmc.cloud.cmclink.doc.mapper.*;
import com.cmc.cloud.cmclink.doc.service.DgCommodityService;
import com.cmc.cloud.cmclink.doc.service.SpecialCargoDgService;
import com.cmc.cloud.cmclink.doc.vo.special.dgextconfigvo.DgConfigVO;
import com.cmc.cloud.cmclink.doc.vo.special.dgextconfigvo.SpecialCargoDgConfBaseVO;
import com.cmc.cloud.cmclink.doc.vo.special.dgextconfigvo.SpecialCargoDgConfPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.special.dgextconfigvo.SpecialCargoDgConfRespVO;
import com.cmc.cloud.cmclink.doc.vo.specialcargocasvo.CasCreateOrUpdateVO;
import com.cmc.cloud.cmclink.doc.vo.specialcargodgpackingvo.PackingCreateOrUpdateVO;
import com.cmc.cloud.cmclink.doc.vo.specialcargodgvo.*;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 特种货审批dg Service 实现类
 *
 * @author 集运管理员
 */
@Service
@Validated
public class SpecialCargoDgServiceImpl implements SpecialCargoDgService {

    @Resource
    private SpecialCargoDgMapper specialCargoDgMapper;
    @Resource
    private SpecialCargoDgPackingMapper specialCargoDgPackingMapper;
    @Resource
    private SpecialCargoCasMapper specialCargoCasMapper;
    @Resource
    private SpecialCargoSegregationMapper specialCargoSegregationMapper;
    @Resource
    private DgCommodityMapper dgCommodityMapper;
    @Resource
    private DgCommodityService dgCommodityService;
    @Resource
    private SpecialCargoDgConfMapper specialCargoDgConfMapper;
    @Resource
    private SpecialCargoSuspectedDgMapper specialCargoSuspectedDgMapper;

    private static final String IMO_CLASS_1 = "1";
    public static final String IMO_CLASS_3 = "3";
    public static final String SUB_RISK_3 = "3";
    public static final String LOGIC_GT = ">=";
    public static final String LOGIC_LT = "<=";
    public static final String LOGIC_EQ = "=";
    public static final String LOGIC_BETWEEN = "Between";

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createSpecialCargoDg(DgCreateOrUpdateReqVO createReqVO) {
        validDgArgs(createReqVO);
        SpecialCargoDgDO specialCargoDg = SpecialCargoDgConvert.INSTANCE.convert(createReqVO);
        if (null != createReqVO.getId()) {
            specialCargoDgMapper.updateById(specialCargoDg);
        } else {
            specialCargoDgMapper.insert(specialCargoDg);
        }
        List<PackingCreateOrUpdateVO> innerPackingList = createReqVO.getInnerPackingList();
        if (CollectionUtil.isNotEmpty(innerPackingList)) {
            innerPackingList.forEach(packing -> {
                SpecialCargoDgPackingDO dgPacking = SpecialCargoDgPackingConvert.INSTANCE.convert(packing);
                dgPacking.setPackingType(PackingEnum.INNER.getValue());
                if (null != packing.getId()) {
                    specialCargoDgPackingMapper.updateById(dgPacking);
                } else {
                    dgPacking.setDgId(specialCargoDg.getId());
                    specialCargoDgPackingMapper.insert(dgPacking);
                }
            });
        }
        List<PackingCreateOrUpdateVO> outerPackingList = createReqVO.getOuterPackingList();
        if (CollectionUtil.isNotEmpty(outerPackingList)) {
            outerPackingList.forEach(packing -> {
                SpecialCargoDgPackingDO dgPacking = SpecialCargoDgPackingConvert.INSTANCE.convert(packing);
                dgPacking.setPackingType(PackingEnum.OUTER.getValue());
                if (null != packing.getId()) {
                    specialCargoDgPackingMapper.updateById(dgPacking);
                } else {
                    dgPacking.setDgId(specialCargoDg.getId());
                    specialCargoDgPackingMapper.insert(dgPacking);
                }
            });
        }
        List<CasCreateOrUpdateVO> casList = createReqVO.getCasList();
        if (CollectionUtil.isNotEmpty(casList)) {
            Map<String, BigDecimal> sumById = casList.stream()
                    .collect(Collectors.groupingBy(
                            CasCreateOrUpdateVO::getCasNoGroup,
                            Collectors.reducing(
                                    BigDecimal.ZERO,
                                    CasCreateOrUpdateVO::getMaxPercent,
                                    BigDecimal::add
                            )
                    ));
            sumById.forEach((id, sum) -> {
                boolean isEqualTo100 = sum.compareTo(BigDecimal.valueOf(100)) == 0;
                if (!isEqualTo100) {
                    throw ServiceExceptionUtil.exception(SpecialCargoApplyErrorCodeConstants.SPECIAL_MAX_CAS_DG_ERROR);
                }
            });
            casList.forEach(cas -> {
                SpecialCargoCasDO dgCas = SpecialCargoCasConvert.INSTANCE.convert(cas);
                if (null != cas.getId()) {
                    specialCargoCasMapper.updateById(dgCas);
                } else {
                    dgCas.setSpecialCargoDgId(specialCargoDg.getId());
                    specialCargoCasMapper.insert(dgCas);
                }
            });
        }
        return specialCargoDg.getId();
    }


    public void validDgArgs(DgCreateOrUpdateReqVO createReqVO) {
        if (StringUtils.isEmpty(createReqVO.getBatteryStatus())) {
            String batteryStatusUnNo = DgCommodityConstants.BATTERY_STATUS_UN_NO;
            String[] split = batteryStatusUnNo.split(",");
            for (String value : split) {
                if (value.equals(createReqVO.getUnNo())) {
                    throw ServiceExceptionUtil.exception(SpecialCargoApplyErrorCodeConstants.BATTERY_STATUS_NOT_FILLED);
                }
            }
        }
        if (StringUtils.isEmpty(createReqVO.getTechnicalName()) || StringUtils.isEmpty(createReqVO.getSegregationGroup())) {
            LambdaQueryWrapperX<DgCommodityDO> queryWrapperX = new LambdaQueryWrapperX<>();
            queryWrapperX.eqIfPresent(DgCommodityDO::getImoClass, createReqVO.getImoClass())
                    .eqIfPresent(DgCommodityDO::getUnNo, createReqVO.getUnNo())
                    .eqIfPresent(DgCommodityDO::getPackingGroup, createReqVO.getPackingGroup())
                    .eqIfPresent(DgCommodityDO::getSubsidiaryRisk, createReqVO.getSubsidiaryRisk())
                    .eqIfPresent(DgCommodityDO::getPsn, createReqVO.getPsn());
            List<DgCommodityDO> dgCommodityDos = dgCommodityMapper.selectList(queryWrapperX);
            boolean hasNos = dgCommodityDos.stream().anyMatch(commodity -> "Y".equals(commodity.getNos()));
            if (hasNos) {
                if (StringUtils.isEmpty(createReqVO.getTechnicalName())) {
                    throw ServiceExceptionUtil.exception(SpecialCargoApplyErrorCodeConstants.TECHNICAL_NAME_NOT_FILLED);
                }
                if (StringUtils.isEmpty(createReqVO.getSegregationGroup())) {
                    throw ServiceExceptionUtil.exception(SpecialCargoApplyErrorCodeConstants.SEGREGATION_GROUP_NOT_FILLED);
                }
            }
        }
        if (null == createReqVO.getFlashPoint() || BigDecimal.ZERO.compareTo(createReqVO.getFlashPoint()) == 0) {
            String imoClass = createReqVO.getImoClass();
            String subRisk = createReqVO.getSubsidiaryRisk();
            if (imoClass.startsWith(IMO_CLASS_3) || subRisk.startsWith(SUB_RISK_3)) {
                throw ServiceExceptionUtil.exception(SpecialCargoApplyErrorCodeConstants.FLASH_POINT_NOT_FILLED);
            }
        }
        if (null == createReqVO.getNetExplosiveWgt() || BigDecimal.ZERO.compareTo(createReqVO.getNetExplosiveWgt()) == 0) {
            String imoClass = createReqVO.getImoClass();
            if (imoClass.startsWith(IMO_CLASS_1)) {
                throw ServiceExceptionUtil.exception(SpecialCargoApplyErrorCodeConstants.NET_EXPLOSIVE_WGT_NOT_FILLED);
            }
        }
    }

    @Override
    public void updateSpecialCargoDg(SpecialCargoDgUpdateReqVO updateReqVO) {
        // 更新
        SpecialCargoDgDO updateObj = SpecialCargoDgConvert.INSTANCE.convert(updateReqVO);
        specialCargoDgMapper.updateById(updateObj);
    }

    @Override
    public void deleteSpecialCargoDg(Long id) {
        // 删除
        specialCargoDgMapper.deleteById(id);
    }

    @Override
    public SpecialCargoDgDO getSpecialCargoDg(Long id) {
        return specialCargoDgMapper.selectById(id);
    }

    @Override
    public List<SpecialCargoDgDO> getSpecialCargoDgList(Long applyId) {
        LambdaQueryWrapperX<SpecialCargoDgDO> queryWrapperX = new LambdaQueryWrapperX<SpecialCargoDgDO>()
                .eqIfPresent(SpecialCargoDgDO::getApplyId, applyId);
        return specialCargoDgMapper.selectList(queryWrapperX);
    }

    @Override
    public PageResult<SpecialCargoDgDO> getSpecialCargoDgPage(SpecialCargoDgPageReqVO reqVO) {
        LambdaQueryWrapperX<SpecialCargoDgDO> queryWrapperX = new LambdaQueryWrapperX<SpecialCargoDgDO>()
                .eqIfPresent(SpecialCargoDgDO::getApplyId, reqVO.getApplyId());
        return specialCargoDgMapper.selectPage(reqVO, queryWrapperX);
    }

    @Override
    public List<String> getSegregation() {
        List<SpecialCargoSegregationDO> segregationDos = specialCargoSegregationMapper.selectList(new LambdaQueryWrapperX<>());
        return segregationDos.stream().map(SpecialCargoSegregationDO::getSegregationGroup).distinct().collect(Collectors.toList());
    }

    @Override
    public List<DgUnNoSimpleRespVO> getUnNo() {
        LambdaQueryWrapperX<DgCommodityDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.eq(DgCommodityDO::getEnable, true);
        queryWrapperX.orderByAsc(DgCommodityDO::getUnNo).last("LIMIT 20");
        List<DgCommodityDO> dgCommodityDos = dgCommodityMapper.selectList(queryWrapperX);
        return dgCommodityDos.stream()
                .map(dg -> {
                    DgUnNoSimpleRespVO voObj = new DgUnNoSimpleRespVO();
                    voObj.setUnNo(null == dg.getUnNo() ? StringUtils.EMPTY : dg.getUnNo());
                    voObj.setImoClass(null == dg.getImoClass() ? StringUtils.EMPTY : dg.getImoClass());
                    voObj.setPsn(null == dg.getPsn() ? StringUtils.EMPTY : dg.getPsn());
                    voObj.setPackingGroup(null == dg.getPackingGroup() ? StringUtils.EMPTY : dg.getPackingGroup());
                    voObj.setNos(null == dg.getNos() ? StringUtils.EMPTY : dg.getNos());
                    voObj.setSubRisk(null == dg.getSubsidiaryRisk() ? StringUtils.EMPTY : dg.getSubsidiaryRisk());
                    voObj.setVersionNo(null == dg.getVersionNo() ? StringUtils.EMPTY : dg.getVersionNo());
                    voObj.setEmsNo(null == dg.getEms() ? StringUtils.EMPTY : dg.getEms());
                    voObj.setMarinePollutant(Boolean.FALSE);
                    String mp = dg.getMp();
                    if (CommonConstants.MARINE_POLLUTANT.equalsIgnoreCase(mp)) {
                        voObj.setMarinePollutant(Boolean.TRUE);
                    }
                    return voObj;
                })
                .collect(Collectors.toList());
    }

    @Override
    public DgUnNoRespVO getImoClassAndPsn(String unNo) {
        LambdaQueryWrapperX<DgCommodityDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.eqIfPresent(DgCommodityDO::getUnNo, unNo);
        List<DgCommodityDO> dgCommodityDos = dgCommodityMapper.selectList(queryWrapperX);
        List<String> imoClassList = dgCommodityDos.stream().map(DgCommodityDO::getImoClass).distinct().collect(Collectors.toList());
        List<String> packGroup = dgCommodityDos.stream().map(DgCommodityDO::getPackingGroup).distinct().collect(Collectors.toList());
        List<String> psns = dgCommodityDos.stream().map(DgCommodityDO::getPsn).distinct().collect(Collectors.toList());
        List<String> psnList = psns.stream().flatMap(s -> Arrays.stream(s.split(","))).map(String::trim).collect(Collectors.toList());
        DgUnNoRespVO dgUnNoRespVO = new DgUnNoRespVO();
        dgUnNoRespVO.setImoClassList(imoClassList);
        dgUnNoRespVO.setPsnList(psnList);
        List<String> packGroupList = packGroup.stream()
                .filter(StringUtils::isNotBlank)
                .collect(Collectors.toList());
        dgUnNoRespVO.setPackList(packGroupList);
        return dgUnNoRespVO;
    }

    @Override
    public DgExtByUnNoVO checkIfDgExtByUnNo(String unNo) {
        LambdaQueryWrapper<DgCommodityDO> queryWrapperX = new LambdaQueryWrapper<>();
        queryWrapperX.eq(DgCommodityDO::getUnNo, unNo);
        queryWrapperX.eq(DgCommodityDO::getEnable, true).last("LIMIT 1");
        DgCommodityDO dgCommodityDo = dgCommodityMapper.selectOne(queryWrapperX);
        Map<String, String> mapDgCommodity = new HashMap<>();
        if (null != dgCommodityDo) {
            transferMap(mapDgCommodity, dgCommodityDo);
        }
        DgExtByUnNoVO dgExtByUnNoRespVo = new DgExtByUnNoVO();
        List<DgConfigVO> dgExts = new ArrayList<>();
        LambdaQueryWrapper<SpecialCargoDgConfDO> queryWrapperConf = new LambdaQueryWrapper<>();
        queryWrapperConf.eq(SpecialCargoDgConfDO::getEnable, true);
        List<SpecialCargoDgConfDO> specialCargoDgConfig = specialCargoDgConfMapper.selectList(queryWrapperConf);
        specialCargoDgConfig.forEach(config -> {
            String valueConf = config.getDgReferValue();
            String valueDo = mapDgCommodity.get(config.getDgReferName());
            if (StringUtils.isNotEmpty(valueDo) && valueConf.equals(valueDo)) {
                dgExts.add(BeanUtil.copyProperties(config, DgConfigVO.class));
            }
        });
        dgExtByUnNoRespVo.setDgExts(dgExts);
        return dgExtByUnNoRespVo;
    }

    @Override
    public void checkDgExtValue(List<DgConfigVO> extArg) {
        if (CollectionUtil.isEmpty(extArg)) {
            return;
        }
        extArg.forEach(arg -> {
            double extArgValue = Double.parseDouble(arg.getExtFiledValue());
            String comparativeLogic = arg.getComparativeLogic();
            String dgExtConfig = arg.getDgExtValue();
            if (LOGIC_BETWEEN.equalsIgnoreCase(comparativeLogic)) {
                String[] parts = dgExtConfig.split("[,，]");
                double num1 = Double.parseDouble(parts[0].trim());
                double num2 = Double.parseDouble(parts[1].trim());
                if (extArgValue > num2 || extArgValue < num1) {
                    throw ServiceExceptionUtil.exception(SpecialCargoDgErrorCodeConstants.SPECIAL_CARGO_DG_VALUE_ERROR);
                }
                return;
            }
            double extValueConf = Double.parseDouble(dgExtConfig);
            if (LOGIC_GT.equals(comparativeLogic)) {
                if (extArgValue < extValueConf) {
                    throw ServiceExceptionUtil.exception(SpecialCargoDgErrorCodeConstants.SPECIAL_CARGO_DG_VALUE_ERROR);
                }
                return;
            }
            if (LOGIC_LT.equals(comparativeLogic)) {
                if (extArgValue > extValueConf) {
                    throw ServiceExceptionUtil.exception(SpecialCargoDgErrorCodeConstants.SPECIAL_CARGO_DG_VALUE_ERROR);
                }
                return;
            }
            if (LOGIC_EQ.equals(comparativeLogic)) {
                if (!new BigDecimal(extArgValue).equals(new BigDecimal(extValueConf))) {
                    throw ServiceExceptionUtil.exception(SpecialCargoDgErrorCodeConstants.SPECIAL_CARGO_DG_VALUE_ERROR);
                }
            }
        });
    }

    @Override
    public PageResult<DgUnNoSimpleRespVO> getUnNoDgPage(UnNoDgPageReqVO pageReqVO) {
        LambdaQueryWrapperX<DgCommodityDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.likeIfPresent(DgCommodityDO::getUnNo, pageReqVO.getUnNo());
        queryWrapperX.likeIfPresent(DgCommodityDO::getImoClass, pageReqVO.getImoClass());
        queryWrapperX.eq(DgCommodityDO::getEnable, true);
        queryWrapperX.orderByAsc(DgCommodityDO::getUnNo);
        PageResult<DgCommodityDO> pageResult = dgCommodityMapper.selectPage(pageReqVO, queryWrapperX);
        List<DgCommodityDO> dgCommodityDos = pageResult.getList();
        List<DgUnNoSimpleRespVO> doCommodity = dgCommodityDos.stream()
                .map(dg -> {
                    DgUnNoSimpleRespVO voObj = new DgUnNoSimpleRespVO();
                    voObj.setUnNo(null == dg.getUnNo() ? StringUtils.EMPTY : dg.getUnNo());
                    voObj.setImoClass(null == dg.getImoClass() ? StringUtils.EMPTY : dg.getImoClass());
                    voObj.setPsn(null == dg.getPsn() ? StringUtils.EMPTY : dg.getPsn());
                    voObj.setPackingGroup(null == dg.getPackingGroup() ? StringUtils.EMPTY : dg.getPackingGroup());
                    voObj.setNos(null == dg.getNos() ? StringUtils.EMPTY : dg.getNos());
                    voObj.setSubRisk(null == dg.getSubsidiaryRisk() ? StringUtils.EMPTY : dg.getSubsidiaryRisk());
                    voObj.setVersionNo(null == dg.getVersionNo() ? StringUtils.EMPTY : dg.getVersionNo());
                    voObj.setEmsNo(null == dg.getEms() ? StringUtils.EMPTY : dg.getEms());
                    voObj.setMarinePollutant(Boolean.FALSE);
                    String mp = dg.getMp();
                    if (CommonConstants.MARINE_POLLUTANT.equalsIgnoreCase(mp)) {
                        voObj.setMarinePollutant(Boolean.TRUE);
                    }
                    return voObj;
                }).collect(Collectors.toList());
        return new PageResult<>(doCommodity, pageResult.getTotal());
    }

    @Override
    public void createSpecialCargoDgConf(SpecialCargoDgConfBaseVO createReqVo) {
        LambdaQueryWrapper<SpecialCargoDgConfDO> queryWrapperX = new LambdaQueryWrapper<>();
        queryWrapperX.eq(SpecialCargoDgConfDO::getDgReferName, createReqVo.getDgReferName())
                .eq(SpecialCargoDgConfDO::getDgReferValue, createReqVo.getDgReferValue()).last("LIMIT 1");
        SpecialCargoDgConfDO specialCargoDgConfDO = specialCargoDgConfMapper.selectOne(queryWrapperX);
        if (null != specialCargoDgConfDO) {
            throw ServiceExceptionUtil.exception(SpecialCargoDgErrorCodeConstants.SPECIAL_CARGO_DG_CONF_EXISTS);
        }
        String comparativeLogic = createReqVo.getComparativeLogic();
        if ("Between".equalsIgnoreCase(comparativeLogic)) {
            createReqVo.setDgExtValue(dgExtValue(createReqVo.getDgExtValue()));
        }
        SpecialCargoDgConfDO specialCargoDgConf = SpecialCargoDgConfConvert.INSTANCE.convert(createReqVo);
        specialCargoDgConfMapper.insert(specialCargoDgConf);
    }

    @Override
    public void updateSpecialCargoDgConf(SpecialCargoDgConfBaseVO updateReqVo) {
        if ("Between".equalsIgnoreCase(updateReqVo.getComparativeLogic())) {
            updateReqVo.setDgExtValue(dgExtValue(updateReqVo.getDgExtValue()));
        }
        SpecialCargoDgConfDO updateObj = SpecialCargoDgConfConvert.INSTANCE.convert(updateReqVo);
        specialCargoDgConfMapper.updateById(updateObj);
    }

    public static String dgExtValue(String dgExtValue) {
        String[] parts = dgExtValue.split("[,，]");
        if (parts.length != 2) {
            throw ServiceExceptionUtil.exception(SpecialCargoDgErrorCodeConstants.SPECIAL_CARGO_DG_BETWEEN_ERROR);
        }
        try {
            double num1 = Double.parseDouble(parts[0].trim());
            double num2 = Double.parseDouble(parts[1].trim());
            String value = num1 + "," + num2;
            if (num1 > num2) {
                value = num2 + "," + num1;
            }
            return value;
        } catch (NumberFormatException e) {
            throw ServiceExceptionUtil.exception(SpecialCargoDgErrorCodeConstants.SPECIAL_CARGO_DG_ARG_ERROR);
        }
    }

    @Override
    public void deleteSpecialCargoDgConf(Long id) {
        specialCargoDgConfMapper.deleteById(id);
    }


    @Override
    public SpecialCargoDgConfDO getSpecialCargoDgConf(Long id) {
        return specialCargoDgConfMapper.selectById(id);
    }


    @Override
    public PageResult<SpecialCargoDgConfRespVO> getSpecialCargoDgConfPage(SpecialCargoDgConfPageReqVO reqVO) {
        LambdaQueryWrapperX<SpecialCargoDgConfDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.likeIfPresent(SpecialCargoDgConfDO::getDgReferName, reqVO.getDgReferName());
        queryWrapperX.likeIfPresent(SpecialCargoDgConfDO::getDgExtName, reqVO.getDgExtName());
        queryWrapperX.orderByDesc(SpecialCargoDgConfDO::getCreateTime);
        PageResult<SpecialCargoDgConfDO> pageResult = specialCargoDgConfMapper.selectPage(reqVO, queryWrapperX);
        List<SpecialCargoDgConfRespVO> results = BeanUtil.copyToList(pageResult.getList(), SpecialCargoDgConfRespVO.class);
        return new PageResult<>(results, pageResult.getTotal());
    }

    @Override
    public void updateSpecialCargoSuspectedDg(SpecialCargoSuspectedDgBaseVO updateReqVO) {
        LambdaQueryWrapper<SpecialCargoSuspectedDgDO> queryWrapperX = new LambdaQueryWrapper<>();
        queryWrapperX.in(SpecialCargoSuspectedDgDO::getId, updateReqVO.getIds());
        List<SpecialCargoSuspectedDgDO> suspectedDgs = specialCargoSuspectedDgMapper.selectList(queryWrapperX);
        if (CollectionUtil.isEmpty(suspectedDgs)) {
            return;
        }
        suspectedDgs.forEach(dg -> {
            dg.setProcessingFlag(updateReqVO.getProcessingFlag());
            dg.setCargoType(updateReqVO.getCargoType());
            dg.setWithdraw(updateReqVO.getWithdraw());
            dg.setRemark(updateReqVO.getRemark());
        });
        specialCargoSuspectedDgMapper.updateBatch(suspectedDgs);
    }

    @Override
    public void deleteSpecialCargoSuspectedDg(Long id) {
        specialCargoSuspectedDgMapper.deleteById(id);
    }

    @Override
    public SpecialCargoSuspectedDgDO getSpecialCargoSuspectedDg(Long id) {
        return specialCargoSuspectedDgMapper.selectById(id);
    }

    @Override
    public PageResult<SpecialCargoSuspectedDgBaseVO> getSpecialCargoSuspectedDgPage(SpecialCargoSuspectedDgPageReqVO reqVO) {
        if (reqVO.getEndDate() != null) {
            reqVO.setEndDate(reqVO.getEndDate().plusDays(1));
        }
        LambdaQueryWrapperX<SpecialCargoSuspectedDgDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.eqIfPresent(SpecialCargoSuspectedDgDO::getCargoType, reqVO.getCargoType());
        queryWrapperX.eqIfPresent(SpecialCargoSuspectedDgDO::getProcessingFlag, reqVO.getProcessingFlag());
        if (null != reqVO.getBeginDate()) {
            queryWrapperX.ge(SpecialCargoSuspectedDgDO::getCreateTime, reqVO.getBeginDate());
        }
        if (null != reqVO.getEndDate()) {
            queryWrapperX.le(SpecialCargoSuspectedDgDO::getCreateTime, reqVO.getEndDate());
        }
        queryWrapperX.orderByDesc(SpecialCargoSuspectedDgDO::getCreateTime);
        PageResult<SpecialCargoSuspectedDgDO> pageResult = specialCargoSuspectedDgMapper.selectPage(reqVO, queryWrapperX);
        List<SpecialCargoSuspectedDgBaseVO> results = BeanUtil.copyToList(pageResult.getList(), SpecialCargoSuspectedDgBaseVO.class);
        return new PageResult<>(results, pageResult.getTotal());
    }


    public void transferMap(Map<String, String> mapDgCommodity, DgCommodityDO dgCommodityDo) {
        mapDgCommodity.put("UNNO", dgCommodityDo.getUnNo());
        mapDgCommodity.put("SEQ", null == dgCommodityDo.getSeq() ? null : dgCommodityDo.getSeq().toString());
        mapDgCommodity.put("PSN", dgCommodityDo.getPsn());
        mapDgCommodity.put("IMDG", dgCommodityDo.getImoClass());
        mapDgCommodity.put("CMPT_GROUP", dgCommodityDo.getCmptGroup());
        mapDgCommodity.put("SUB_RISK", dgCommodityDo.getSubsidiaryRisk());
        mapDgCommodity.put("STOWAGE_CATEGORY", dgCommodityDo.getStowageCategory());
        mapDgCommodity.put("EXT_IMDG", dgCommodityDo.getExtImdg());
        mapDgCommodity.put("SEGREGATION_GROUP", dgCommodityDo.getSegregationGroup());
        mapDgCommodity.put("SEGREGATION_TABLE", dgCommodityDo.getSegregationTable());
        mapDgCommodity.put("SEGREGATION_TABLE2", dgCommodityDo.getSegregationTable2());
        mapDgCommodity.put("SPECIAL_PROV", dgCommodityDo.getSpecialProv());
        mapDgCommodity.put("PACK_GROUP", dgCommodityDo.getPackingGroup());
        mapDgCommodity.put("MP", dgCommodityDo.getMp());
        mapDgCommodity.put("CLEAR_LIVING", dgCommodityDo.getClearLiving());
        mapDgCommodity.put("FOODSTUFFS", dgCommodityDo.getFoodstuffs());
        mapDgCommodity.put("HEAT_SOURCE", dgCommodityDo.getHeatSource());
        mapDgCommodity.put("SHADE_RADIENT", dgCommodityDo.getShadeRadient());
        mapDgCommodity.put("FLASHPOINT", null == dgCommodityDo.getFlashPoint() ? null : dgCommodityDo.getFlashPoint().toString());
        mapDgCommodity.put("STOWAGE_SEGREGATION", dgCommodityDo.getStowageSegregation());
        mapDgCommodity.put("LIMITED_QTY", dgCommodityDo.getLimitedQty());
        mapDgCommodity.put("PACK_INSTR", dgCommodityDo.getPackInstr());
        mapDgCommodity.put("PACK_PROV", dgCommodityDo.getPackProv());
        mapDgCommodity.put("IBC_INSTR", dgCommodityDo.getIbcInstr());
        mapDgCommodity.put("IBC_PROV", dgCommodityDo.getIbcProv());
        mapDgCommodity.put("TANK_IMO", dgCommodityDo.getTankImo());
        mapDgCommodity.put("TANK_UN", dgCommodityDo.getTankUn());
        mapDgCommodity.put("TANK_PROV", dgCommodityDo.getTankProv());
        mapDgCommodity.put("EMS", dgCommodityDo.getEms());
        mapDgCommodity.put("NOT_MIX_CLASS1", dgCommodityDo.getNotMixClass1());
        mapDgCommodity.put("CARGO_TYPE", dgCommodityDo.getCargoType());
        mapDgCommodity.put("FP_GROUP", dgCommodityDo.getFpGroup());
        mapDgCommodity.put("STOWAGE_HANDLING", dgCommodityDo.getStowageHandling());
        mapDgCommodity.put("SEGREGATION", dgCommodityDo.getSegregation());
        mapDgCommodity.put("NOS", dgCommodityDo.getNos());
        mapDgCommodity.put("ID", dgCommodityDo.getBusinessId());
        mapDgCommodity.put("ID1", dgCommodityDo.getBusinessId1());
    }



    @Override
    public void saveSuspectedDg(List<SuspectedDgReqVO> reqVo) {

    }
}
